class Storage {

    constructor() { }

    // 单例
    static of() {
        if (!this._instance) {
            this._instance = new Storage()
        }
        return this._instance
    }

    // 存储
    set(key, val) {
        if (val == null || val == undefined || val == 'undefined') {
            localStorage.setItem(key, val)
        } else {
            localStorage.setItem(key, this.stringify(val))
        }
    }

    // 获取
    get(key) {
        let val = localStorage.getItem(key)
        if (val == null || val == undefined || val == 'undefined') {
            return ''
        } else {
            return this.parse(val)
        }
    }

    // 删除一个
    removeOne(key) {
        if (!key) {
            return
        }
        localStorage.removeItem(key)
    }

    // 删除所有
    removeAll() {
        localStorage.clear()
    }

    // 转换JSON串
    stringify(val) {
        return JSON.stringify(val)
    }

    // 解析JSON串
    parse(val) {
        return JSON.parse(val)
    }

}
export default Storage